<?php

namespace App\backend\controllers\menu;


use App\backend\controllers\AdminBaseController;
use App\backend\models\MenuModel;
use App\backend\services\MenuServices;
use App\Exceptions\InvalidRequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Utils\JsonResp;

class MenuController extends AdminBaseController
{
    protected $class_name = 'Menu';

    /**
     * 获取 - 权限路由菜单列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenuRoleList(Request $request)
    {
        $data = $request->all();
        $list = MenuServices::getMenuRoleList($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 获取 - 菜单列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function lists(Request $request)
    {
        $data = $request->all();
        $list = MenuServices::getList($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 获取 - 菜单详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            MenuModel::update_rules(),
            MenuModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = MenuServices::getDetail($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑 - 菜单
     */
    public function modify(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            MenuModel::modify_rules(),
            MenuModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = MenuServices::modify($data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }
        return JsonResp::success('成功', []);
    }

    /**
     * 删除 - 菜单
     */
    public function delete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            MenuModel::update_rules(),
            MenuModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = MenuServices::delete($data);
        return JsonResp::success('成功', $list);
    }

}
